import 'package:flutter/material.dart';
import 'package:hi_flutter/app_page.dart';

class LifecyclePage extends StatefulWidget {
  const LifecyclePage({Key? key}) : super(key: key);

  @override
  State<LifecyclePage> createState() => _LifecyclePageState();
}

class _LifecyclePageState extends State<LifecyclePage>
    with WidgetsBindingObserver {
  AppLifecycleState? lastLifecycleState;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // print("didChangeAppLifecycleState :" + state.name);
    setState(() {
      lastLifecycleState = state;
    });
  }

  Widget buildContent() {
    if (lastLifecycleState == null) {
      return const Text(
        'This widget has not observed any lifecycle changes.',
        textDirection: TextDirection.ltr,
      );
    }
    return Text(
      'The most recent lifecycle state this widget observed was: $lastLifecycleState.',
      textDirection: TextDirection.ltr,
    );
  }

  @override
  Widget build(BuildContext context) {
    return AppPage(
      body: Center(
        child: buildContent(),
      ),
    );
  }
}
